package com.dimao.certificate_build_service.pdf;

import com.dimao.certificate_build_service.Util.asyncReadFile.ReaderFactory;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;

public class PDFConfig {
    private String basePDFPath;
    private String savePDFPath;
    private String saveTemporaryPDFPath;
    private String fontPath;

    public PDFConfig(){
        this.setBasePDFPath("assets/template")
                .setSavePDFPath("pdf/keep")
                .setSaveTemporaryPDFPath("pdf/temporary")
                .setFontPath("assets/font");
    }

    private PDFConfig(JsonObject jsonObject){
        this();

        if(!jsonObject.isEmpty()){
            if(jsonObject.containsKey("base_pdf_path"))
                setBasePDFPath(jsonObject.getString("base_pdf_path"));
            if(jsonObject.containsKey("save_pdf_path"))
                setSavePDFPath(jsonObject.getString("save_pdf_path"));
            if(jsonObject.containsKey("save_temporary_pdf_path"))
                setSaveTemporaryPDFPath(jsonObject.getString("save_temporary_pdf_path"));
            if(jsonObject.containsKey("font_path"))
                setFontPath(jsonObject.getString("font_path"));
        }
    }

    public static Future<PDFConfig> loadInFile(Vertx vertx, String file){
        return Future.future(promise ->
                ReaderFactory.produceJsonReader(vertx)
                        .read(file)
                        .execute(result -> {
                            if(result.failed()) promise.fail(result.cause());
                            else promise.complete(new PDFConfig(result.result()));
                        })
        );
    }

    public String getBasePDFPath() {
        return basePDFPath;
    }

    public PDFConfig setBasePDFPath(String basePDFPath) {
        this.basePDFPath = basePDFPath;
        return this;
    }

    public String getSavePDFPath() {
        return savePDFPath;
    }

    public PDFConfig setSavePDFPath(String savePDFPath) {
        this.savePDFPath = savePDFPath;
        return this;
    }

    public String getSaveTemporaryPDFPath() {
        return saveTemporaryPDFPath;
    }

    public PDFConfig setSaveTemporaryPDFPath(String saveTemporaryPDFPath) {
        this.saveTemporaryPDFPath = saveTemporaryPDFPath;
        return this;
    }

    public String getFontPath() {
        return fontPath;
    }

    public PDFConfig setFontPath(String fontPath) {
        this.fontPath = fontPath;
        return this;
    }
}
